package com.bkgy.modular.gzTechnologyDetermine.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.datacollect_bx.bean.JDataHotcrown;
import com.bkgy.modular.datacollect_bx.bean.JDataSave653;
import com.bkgy.modular.gzTechnologyDetermine.entity.ExportGztechnologyDetermine;
import com.bkgy.modular.gzTechnologyDetermine.entity.GztechnologyDetermine;
import com.bkgy.modular.gzTechnologyDetermine.entity.GztechnologyDeterminepd;
import com.bkgy.modular.gzTechnologyDetermine.entity.PlanCheckPdi653;
import com.bkgy.modular.gzTechnologyDetermine.mapper.GztechnologyDetermineMapper;
import com.bkgy.modular.gzTechnologyDetermine.service.GztechnologyDetermineService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GztechnologyDetermineServiceImpl extends ServiceImpl<GztechnologyDetermineMapper, GztechnologyDetermine> implements GztechnologyDetermineService {

    @Resource
    private GztechnologyDetermineMapper gztechnologyDetermineMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResultData findByData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");

        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<GztechnologyDetermine> list = gztechnologyDetermineMapper.findByPageData((pageIndex - 1) * pageSize, pageIndex * pageSize, condition);
        Integer count = gztechnologyDetermineMapper.findByPageDataCount(condition);
        return ResultData.ResultDataSuccess(list, count);
    }

    @Override
    public ResultData findByDatapd(JSONObject jsonObject) {
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (condition == null) {
            return ResultData.ResultDataFaultSelf("condition参数没有传", null);
        }
        List<GztechnologyDeterminepd> list = null;// technologyDetermineMapper.findByPageDatapd(condition);
        return ResultData.ResultDataSuccess(list);
    }

    @Override
    public ResultData findByMongDbData(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        if (StringUtils.isEmpty(extid)) {
            return ResultData.ResultDataFaultSelf("参数有误", -1);
        }
        Query query = new Query(Criteria.where("extid").is(extid));
        JDataSave653 jDataSave653 = mongoTemplate.findOne(query, JDataSave653.class);
        HashMap<String, List<Double>> map = new HashMap<>();
        List<Double> list1 = jDataSave653.getRow().get("c653_tbs_sv");
        List<Double> list2 = jDataSave653.getRow().get("c653_tbs_pv");
        List<Double> list3 = jDataSave653.getRow().get("c653_spmelong_sv");
        List<Double> list4 = jDataSave653.getRow().get("c653_spmelong_pv");
        List<Double> list5 = jDataSave653.getRow().get("c653_width_en_act");
        List<Double> list6 = jDataSave653.getRow().get("c653_width_ex_value");
        List<Double> list7 = jDataSave653.getRow().get("c653_thk_value");
        List<Double> list8 = jDataSave653.getRow().get("c653_zweight_up");
        List<Double> list9 = jDataSave653.getRow().get("c653_zweight_low");
        List<Double> list10 = jDataSave653.getRow().get("c653_oilcoat_upv");
        List<Double> list11 = jDataSave653.getRow().get("c653_oilcoat_lpv");

        map.put("c653_tbs_sv", list1);
        map.put("c653_tbs_pv", list2);
        map.put("c653_spmelong_sv", list3);
        map.put("c653_spmelong_pv", list4);
        map.put("c653_width_en_act", list5);
        map.put("c653_width_ex_value", list6);
        map.put("c653_thk_value", list7);
        map.put("c653_zweight_up", list8);
        map.put("c653_zweight_low", list9);
        map.put("c653_oilcoat_upv", list10);
        map.put("c653_oilcoat_lpv", list11);

        return ResultData.ResultDataFaultSelf("查询成功", map);
    }

    @Override
    public ResultData findByMongDbDatahotcrown(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        if (StringUtils.isEmpty(extid)) {
            return ResultData.ResultDataFaultSelf("参数有误", -1);
        }
        Query query = new Query(Criteria.where("extid").is(extid));
        JDataHotcrown jDataHotcrown = mongoTemplate.findOne(query, JDataHotcrown.class);
        HashMap<String, List<Double>> map = new HashMap<>();
        List<Double> list1 = jDataHotcrown.getCw().get("c40_c25");
        List<Double> list2 = jDataHotcrown.getCw().get("c100_c25");
        List<Double> list3 = jDataHotcrown.getCw().get("thk_ds25");
        List<Double> list4 = jDataHotcrown.getCw().get("thk_ws25");
        List<Double> list5 = jDataHotcrown.getCw().get("thk_ds40");
        List<Double> list6 = jDataHotcrown.getCw().get("thk_ws40");
        List<Double> list7 = jDataHotcrown.getCw().get("thk_ds70");
        List<Double> list8 = jDataHotcrown.getCw().get("thk_ws70");
        List<Double> list9 = jDataHotcrown.getCw().get("thk_ds100");
        List<Double> list10 = jDataHotcrown.getCw().get("thk_ws100");


        map.put("c40_c25", list1);
        map.put("c100_c25", list2);
        map.put("thk_ds25", list3);
        map.put("thk_ws25", list4);
        map.put("thk_ds40", list5);
        map.put("thk_ws40", list6);
        map.put("thk_ds70", list7);
        map.put("thk_ws70", list8);
        map.put("thk_ds100", list9);
        map.put("thk_ws100", list10);


        return ResultData.ResultDataFaultSelf("查询成功", map);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<GztechnologyDetermine> listExcel = new ArrayList<>();// 调用service查询出数据
        List<GztechnologyDetermine> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = gztechnologyDetermineMapper.findByPageData((i - 1) * pageSize, i * pageSize, condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (GztechnologyDetermine item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportGztechnologyDetermine> list = new ArrayList<>();
        for (GztechnologyDetermine item : listExcel) {
            ExportGztechnologyDetermine exportTechnologyDetermine = new ExportGztechnologyDetermine();
            BeanUtils.copyProperties(item, exportTechnologyDetermine);
            list.add(exportTechnologyDetermine);
        }
        try {
            Map<String, Object> data1 = new HashMap();
            data1.put("data", list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\镀锌工艺判定.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(), false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames, response, workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public ResultData findDataByPage(JSONObject jsonObject)
    {

            JSONObject condition = jsonObject.getJSONObject("condition");
            // (pageIndex - 1) * pageSize, pageIndex * pageSize,
            List<PlanCheckPdi653> list = gztechnologyDetermineMapper.findDataByPage(condition);
            Integer count = gztechnologyDetermineMapper.findDataCount(condition);
            return ResultData.ResultDataSuccess(list, count);

    }
    @Override
    public ResultData findDataHisByPage(JSONObject jsonObject){
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 判断pageIndex和pageSize是否有值,没值给出提示
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        List<PlanCheckPdi653> list = gztechnologyDetermineMapper.findDataHisByPage((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = gztechnologyDetermineMapper.findDataHisCount(condition);
        return ResultData.ResultDataSuccess(list, count);

    }

}